 /*
 * Global.java
 *
 * Created on July 5, 2005, 3:58 AM
 *
 * To change this basis, choose Tools | Options and locate the basis under
 * the Source Creation and Management node. Right-click the basis and choose
 * Open. You can then make changes to the basis in the Source Editor.
 */

package com.xucia.jsponic.data;


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;



/**
 *
 * @author Kris Zyp
 */
public class GlobalData  {
	public static final String CONTAINS_REPEATING_IDENTIFIER = ":isList";
	public static final String TEMPLATES_FIELD = "templates"; 
    static final String CONFIGURATION_FILE_PATH_FROM_WEB_INF = "/classes/DataSources.json";
	public static String getWebInfLocation() {
		String sourcesConfigurationFileLocation = GlobalData.class.getClassLoader().getResource("DataSources.json").toString();
		// remove the last two paths
		if (sourcesConfigurationFileLocation.startsWith("jar:")) {
			sourcesConfigurationFileLocation= sourcesConfigurationFileLocation.substring("jar:".length());
			sourcesConfigurationFileLocation = sourcesConfigurationFileLocation.substring(0,sourcesConfigurationFileLocation.lastIndexOf('/'));
		}
		sourcesConfigurationFileLocation = sourcesConfigurationFileLocation.substring(0,sourcesConfigurationFileLocation.lastIndexOf('/'));
		sourcesConfigurationFileLocation = sourcesConfigurationFileLocation.substring(0,sourcesConfigurationFileLocation.lastIndexOf('/'));
		sourcesConfigurationFileLocation = sourcesConfigurationFileLocation.replaceAll("%20", " ");
		return sourcesConfigurationFileLocation;
	}
	public static final String ID_FIELD_PREFIX = "id:";
	public final static String CHILDMODS_FIELD = "po$childMods";	
	public final static String ALTERS_FIELD = ":alters";
	public static final String DEFAULT_WEBSITE = "default";
    public static final String HTML_FIELD_NAME = "html";
    public static final String LINK_ELEMENT_NAME = "Link"; 
    public static final Object ERASURE_VALUE = new Object(); 
    public static final String TYPE_FIELD = new String("field type");
    public static final String XML_FIELD = new String(HTML_FIELD_NAME);
    public static final String NAME_FIELD = new String("name");
    public static final String PARENT_FIELD = new String("parent");
    public static final String VERSION_NUMBER_FIELD = new String(":version");
    public static final String EDIT_MENU_FIELD = new String("edit menu");
    public static final String CONTEXT_OBJECT_FIELD = new String("contextObject");
    public static final String IS_TEMPLATE_FIELD = new String("isBasis");
    public static final String OBJECT_REPOSITORY_FIELD = new String("object repository"); // this should not be necessary
    public static final String OBJECTS_FIELD = new String("objects");
    public static final String CONTENT_FIELD = new String("content");
    public static final String WEBROOT_FIELD = new String("webroot");
    public static final String TAG_LIBRARY_FIELD = new String("tag library");
    public static final String COMPONENTS_FIELD = new String("components");
    public static final String CLASS_NAME_FIELD = new String("className");
    //public static final String JAVA_CLASS_FIELD = new String("java class");
    //TODO: these two should be merged
    public static final String REQUIRED_TEMPLATE_FIELD = new String("required basis");
    public static final String LIST_REQUIRED_TEMPLATE = new String("list required basis");
    public static final String BASIS_FIELD = new String("basis");
    public static final String VERSION_OF_FIELD = new String("versionOf");
    public static final String COMPONENT_FIELD= new String("component");
    public static final String DATA_BOUND_FIELD = new String("DataBound");
    public static final String MAPPING_FIELD = new String("mapping");
    public static final String TEMPLATE_MAPS_FIELD = new String("basis maps");
    public static final String ACCESS_FIELD = new String("access");
    public static final String ROLE_PERMITS_FIELD = new String("rolePermits");
    public static final String ROLE_FIELD = new String("role");
    public static final String PUBLIC_PERMISSION_FIELD = new String("public");
    public static final String PERMISSION_LEVEL_FIELD = new String("permissionLevel");
  //  public static final String APPEND_FIELD = new String("append");
//    public static final String READ_FIELD = new String("read");
    public static final String LEVEL_FIELD = new String("level");
    //public static final String WRITE_FIELD = new String("write");
   // public static final String VERSION_FIELD = new String("version"); 
    public static final String USER_Id_FIELD = new String("userid"); 
    public static final String USER_NAME_FIELD = new String("username"); 
    public static final String PASSWORD_FIELD = new String(":password"); 
    public static final String USER_TABLE_FIELD = new String("userTable"); 
    public static final String USERS_FIELD = new String("users"); 
    public static final String MEMBERS_FIELD = new String("members"); 
    public static final String USER_INFO_FIELD = new String("userInfo"); 
    public static final String USER_DATA_FIELD = new String("userData"); 
    public static final String GROUPS_FIELD = new String("groups"); 
    public static final String IN_GROUPS_FIELD = new String(":inGroups"); 
    public static final String USER_TEMPLATE_FIELD = new String("userBasis"); 
    public static final String GROUP_TEMPLATE_FIELD = new String("groupBasis"); 
    public static final String CREATION_DATE_FIELD = new String("date created"); 
    public static final String HISTORY_FIELD = new String("history"); 
    public static final String HISTORY_IDENTIFIER_FIELD = ":isHistory";
    public static final String FIELDS_FIELD = new String("fields"); 
    public static final String IMPORT_MAPS = "importMaps";
    public static final String FUNCTION_COMPILED_CODE_FIELD = "psv15"; 
    public static final String FUNCTION_METHOD_FIELD = "function";  
    public static final String FUNCTION_RUN_AT_FIELD = "runAt";  
    public static final String FUNCTION_RUN_AT_SERVER = "server";  
    public static final String FUNCTION_RUN_AT_BOTH = "both";  
    public static final String FUNCTION_RUN_AT_CLIENT = "client";  
    public static final String ALWAYS_DOWNLOAD_FUNCTION= "alwaysDownload"; 
    static Scriptable globalScope;
    public static Scriptable getGlobalScope() {
    	if (globalScope == null) {
			Context cx= Context.enter();
			globalScope = cx.initStandardObjects();
			globalScope.put("pjs", globalScope, new PjsLibrary());
    	}
    	return globalScope;
    }
	/*public static Identifiable getById(String id) {
		return getById(new ObjectId(id));
	}
	public static Identifiable getById(ObjectId id) {
    	String filter = null;
		String [] parts = id.objectId.split("\\|",2);
		if (parts.length > 1)
		{
			id.objectId = parts[0];
			filter= parts[1];
		}
		parts = id.objectId.split(",");
    	ObjectId alterationProto = null;
		if (parts.length > 1)
		{			
			id.objectId = parts[0];
			alterationProto = new ObjectId(parts[1]);			
		}
        Identifiable obj = DataSourceManager.getById(id);
        if (!(obj instanceof ManagedSource))
        	return obj;
        obj = (Identifiable) DataObjectWrapper.wrapper(Persistent.class,obj);
        if (alterationProto != null)
        	obj = ((DescendantsInheriter) obj).getAlterationById(alterationProto);

        if (filter != null) {
        	return ((ListDataObjectCapable) obj).filter(MultiStipulation.createStipulation(filter));
        }
        return obj;

	}*/

    public static void printStackTrace() {
        try { throw new RuntimeException("here"); } catch (Exception e) { e.printStackTrace(); }
    }
/*    private static final String XML_DATA_PATH="xmldata";

    //public static User anonymous;
	public static ServletContext servletContext;
	public static void initializeContext(ServletContext servletContextParam) {
        if (servletContextParam != null) {
        	GlobalData.servletContext = servletContextParam; 
            String initParamPath = GlobalData.servletContext.getInitParameter("rootDataPath");
            if (initParamPath != null)
            	AbstractDataSource.rootXMLPath = initParamPath;
            else
            	AbstractDataSource.rootXMLPath = GlobalData.servletContext.getRealPath(XML_DATA_PATH);
        }
	}*/
}
